package com.anda.logistics.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.anda.logistics.dao.TruckInfoDao;
import com.anda.logistics.model.SystemUser;
import com.anda.logistics.model.TruckInfo;
import com.anda.logistics.model.TruckInfoHis;
import com.anda.logistics.service.TruckInfoService;
import com.anda.logistics.util.JQGridPager;
import com.anda.logistics.util.QueryFilter;
import com.anda.logistics.vo.JsonReturnVo;

@Component
public class TruckInfoServiceImpl implements TruckInfoService{
	
	private TruckInfoDao truckInfoDao;
	
	@Override
	public void save(TruckInfo truckInfo, SystemUser user) {
		Date now = new Date();
		truckInfo.setMakeTime(now);
		truckInfo.setCreateUserId(user.getId());
		truckInfo.setCreateUserName(user.getUserName());
		truckInfoDao.save(truckInfo);
		TruckInfoHis his = copyToHis(truckInfo, user, now, 1);
		truckInfoDao.save(his);
	}

	private TruckInfoHis copyToHis(TruckInfo ti, SystemUser user, Date d, int modifyType) {
		TruckInfoHis his = new TruckInfoHis();
		his.setAuditTime(ti.getAuditTime());
		his.setAuditUserId(ti.getAuditUserId());
		his.setAuditUserName(ti.getAuditUserName());
		his.setBodyStruc(ti.getBodyStruc());
		his.setCapacity(ti.getCapacity());
		his.setCreateUserId(user.getId());
		his.setCreateUserName(user.getUserName());
		his.setDriverName(ti.getDriverName());
		his.setDriverSex(ti.getDriverSex());
		his.setDrivingLicense(ti.getDrivingLicense());
		his.setFreqLine(ti.getFreqLine());
		his.setLicenseType(ti.getLicenseType());
		his.setLocCity(ti.getLocCity());
		his.setLocProvince(ti.getLocProvince());
		his.setLocRegion(ti.getLocRegion());
		his.setMobileNo(ti.getMobileNo());
		his.setModifyTime(d);
		his.setModifyType(modifyType);
		his.setPlateNo(ti.getPlateNo());
		his.setRunningToken(ti.getRunningToken());
		his.setTruckBrand(ti.getTruckBrand());
		his.setTruckInfoId(ti.getId());
		his.setTruckLength(ti.getTruckLength());
		his.setTruckState(ti.getTruckState());
		his.setTruckType(ti.getTruckType());
		his.setTruckVolumn(ti.getTruckVolumn());
		
		return his;
	}

	public TruckInfoDao getTruckInfoDao() {
		return truckInfoDao;
	}
	
	@Resource
	public void setTruckInfoDao(TruckInfoDao truckInfoDao) {
		this.truckInfoDao = truckInfoDao;
	}

	@Override
	public List<TruckInfo> getAll() {
		return this.truckInfoDao.getAll();
	}

	@Override
	public TruckInfo getOne(long id) {
		return this.truckInfoDao.getOne(id);
	}

	@Override
	public void deleteOne(long id, SystemUser user) {
		TruckInfo ti = truckInfoDao.getOne(id);
		TruckInfoHis his = copyToHis(ti, user, new Date(), 3);
		truckInfoDao.save(his);
		truckInfoDao.delete(ti);
	}

	@Override
	public void update(TruckInfo truckInfo, SystemUser user) {
		Date now = new Date();
		TruckInfo old = truckInfoDao.getOne(truckInfo.getId());
		old.setBodyStruc(truckInfo.getBodyStruc());
		old.setCapacity(truckInfo.getCapacity());
		old.setDriverName(truckInfo.getDriverName());
		old.setDriverSex(truckInfo.getDriverSex());
		old.setDrivingLicense(truckInfo.getDrivingLicense());
		old.setFreqLine(truckInfo.getFreqLine());
		old.setLicenseType(truckInfo.getLicenseType());
		old.setLocCity(truckInfo.getLocCity());
		old.setLocProvince(truckInfo.getLocProvince());
		old.setLocRegion(truckInfo.getLocRegion());
		old.setModifyTime(now);
		old.setMobileNo(truckInfo.getMobileNo());
		old.setPlateNo(truckInfo.getPlateNo());
		old.setRunningToken(truckInfo.getRunningToken());
		old.setTruckBrand(truckInfo.getTruckBrand());
		old.setTruckLength(truckInfo.getTruckLength());
		old.setTruckType(truckInfo.getTruckType());
		old.setTruckVolumn(truckInfo.getTruckVolumn());
		
		truckInfoDao.update(old);
		TruckInfoHis his = copyToHis(old, user, now, 2);
		truckInfoDao.save(his);
	}

	@Override
	public List<TruckInfo> getPagerList(JQGridPager pager,String state) {
		return this.truckInfoDao.getPagerList(pager,state);
	}

	@Override
	public long getRecords() {
		return this.truckInfoDao.getRecords();
	}

	@Override
	public List getTruckCountByProvince() {
		return truckInfoDao.getTruckCountByProvince();
	}

	@Override
	public long getPagedRecords(JQGridPager pager,String state) {
		return this.truckInfoDao.getPagedRecords(pager,state);
	}

	@Override
	public int updateAuditState(String[] ids, SystemUser user) {
		int result = 0;
		TruckInfo ti = null;
		TruckInfoHis his = null;
		Date now = new Date();
		for(String id: ids){
			ti = truckInfoDao.getOne(Integer.parseInt(id));
			ti.setAuditTime(now);
			ti.setAuditUserId(user.getId());
			ti.setAuditUserName(user.getUserName());
			ti.setTruckState(1);
			truckInfoDao.update(ti);
			result += 1;
			
			his = copyToHis(ti, user, now, 4);
			truckInfoDao.save(his);
		}
		return result;
	}

	@Override
	public TruckInfo getOneByMobileNo(String sMobileNo) {
		return truckInfoDao.getOneByMobileNo(sMobileNo);
	}

	@Override
	public JsonReturnVo getListByQuery(QueryFilter qf) {
		return truckInfoDao.getListByQuery(qf);
	}
	
}
